﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CFScript
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public abstract class Task:Instruction
    {
        public override string ToString()
        {
            StringBuilder result = new StringBuilder();

            result.AppendFormat("<{0}{1}{2}", base.name, GenerateTaskAttributes(), GenerateTaskOptions(), GeneratTaskEnd());

            return result.ToString();
        }

        private string GenerateTaskAttributes()
        {
            StringBuilder result = new StringBuilder();

            foreach (string key in base.parameters.Keys)
            {
                result.AppendFormat(" {0}='{1}'", key, base.parameters[key]);
            }

            return result.ToString();
        }

        private string GenerateTaskOptions()
        {
            StringBuilder result = new StringBuilder();

            result.AppendFormat(" onError = '{0}'", base.options.OnError.ToString());

            return result.ToString();
        }

        private string GeneratTaskEnd()
        {
            StringBuilder result = new StringBuilder();

            if (base.hasInnerText)
            {
                result.AppendFormat(">\n{0}\n</{1}>", base.innerText, base.name);
            }
            else
            {
                result.Append("/>");
            }

            return result.ToString();
        }
    }
}
